<template>
<!--  TODO 修正编辑框中逻辑错误的代码-->
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="上级菜单">
            <TreeSelect
                v-model="form.parentId"
                :options="menuOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级菜单"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item v-if="id === null" label="菜单类型" prop="menuType">
            <el-radio-group v-model="form.type">
              <el-radio label="L">目录</el-radio>
              <el-radio label="M">菜单</el-radio>
              <el-radio label="B">按钮</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item v-if="form.type !== 'B'" label="菜单图标">
            <el-popover
                placement="bottom-start"
                width="460"
                trigger="click"
                @show="$refs['iconSelect'].reset()"
            >
              <IconSelect ref="iconSelect" @selected="selected"/>
              <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标" readonly>
                <i
                    v-if="form.icon"
                    slot="prefix"
                    :class="form.icon"
                    class="el-input__icon"
                    style="height: 32px;width: 16px;"
                />
                <i v-else slot="prefix" class="el-icon-search el-input__icon"/>
              </el-input>
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜单名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入菜单名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示排序" prop="sort">
            <el-input-number v-model="form.sort" controls-position="right" :min="0" size="small"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.type !== 'B'" prop="path">
              <span slot="label">
                <el-tooltip content="访问的组件路径，如：`system/user`，默认在`views`目录下" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                路由路径
              </span>
            <el-input v-model="form.path" placeholder="请输入路由路径"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.type === 'M'">
          <el-form-item prop="component">
              <span slot="label">
                <el-tooltip content="访问的组件路径，如：`system/user/index`，默认在`views`目录下" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                组件路径
              </span>
            <el-input v-model="form.component" placeholder="请输入组件路径" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <el-input v-model="form.permission" placeholder="请输入权限标识" maxlength="100"/>
            <span slot="label">
                <el-tooltip content="控制器中定义的权限字符，如：@PreAuthorize(`@ss.hasPermi('system:user:list')`)" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                权限字符
              </span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.type === 'M'">
            <el-input v-model="form.query" placeholder="请输入路由参数" maxlength="255"/>
            <span slot="label">
                <el-tooltip content='访问路由的默认传递参数，如：`{"id": 1, "name": "ry"}`' placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                路由参数
              </span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.type !== 'B'">
              <span slot="label">
                <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                显示状态
              </span>
            <el-radio-group v-model="form.visible">
              <el-radio
                  v-for="dict in visibleOptions"
                  :key="dict.value"
                  :label="dict.value"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.type !== 'B'">
              <span slot="label">
                <el-tooltip content="选择停用则路由将不会出现在侧边栏，也不能被访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                菜单状态
              </span>
            <el-radio-group v-model="form.status">
              <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.value"
                  :label="dict.value"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button type="success" size="small" @click="submit">提交</el-button>
          <el-button type="danger" size="small" @click="cancel">取消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {get, post} from "@/plugins/axios"
import TreeSelect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css"
import IconSelect from "@/components/IconSelect"
import {Message} from "element-ui";

export default {
  name: "edit",
  created() {
    if (this.id !== null) {
      this.getMenuData()
    }
    this.form.parentId = this.parentId
    this.getTreeSelect()
  },
  components: { TreeSelect, IconSelect },
  props: ['id', 'parentId'],
  data() {
    const module = '/menu'
    return {
      // 表单数据
      form: {
        parentId: 0,
        type: "L",
        icon: null,
        sort: 0,
        visible: 1,
        status: 1
      },
      // 菜单树下拉列表
      menuOptions:[],
      // 显示状态的单选选项
      visibleOptions: [
        {label: '显示', value: 1},
        {label: '隐藏', value: 0}
      ],
      // 菜单状态的单选选项
      statusOptions: [
        {label: '启用', value: 1},
        {label: '停用', value: 0}
      ],
      // 表单校验规则
      rules: {
        name: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "菜单顺序不能为空", trigger: "blur" }
        ],
        path: [
          { required: true, message: "路由地址不能为空", trigger: "blur" }
        ]
      },
      uri: {
        selectDetail: module + '/selectOne/',
        selectTree: module + '/menuTreeLabel',
        saveOrUpdate: module + '/saveOrUpdate'
      }
    }
  },
  methods: {
    // 查询菜单详情
    getMenuData() {
      get(this.uri.selectDetail + this.id, {}, response => {
        if (response.code === 200) {
          this.form = response.data
        }
      })
    },
    // 选择图标
    selected(name) {
      this.form.icon = name
    },
    // 查询菜单下拉树结构
    getTreeSelect() {
      get(this.uri.selectTree, {}, response => {
        if (response.code === 200) {
          const menu = { id: 0, label: '主类目', children: [] };
          menu.children = response.data
          this.menuOptions.push(menu)
        }
      })
    },
    // 转换菜单数据结构
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.label,
        children: node.children
      };
    },
    // 提交表单
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.icon === null) {
            this.form.icon = ''
          }
          post(this.uri.saveOrUpdate, this.form, response => {
            if (response.code === 200) {
              Message.success(response.message)
            }
            this.$emit('update:editDialogShow',false)
            this.$emit('getMenuTree')
          })
        } else {
          return false
        }
      })
    },
    // 取消提交
    cancel() {
      this.$emit('update:editDialogShow',false)
    }
  }
}
</script>

<style scoped lang="less">

</style>